use lib "../";
use BuildTool;


use Test::More tests => 9;
use Test::Exception;
use Cwd;
Test::More->builder->output ('Test_Report/Test_Cases/ts6_result.txt');
Test::More->builder->failure_output ('Test_Report/Test_Cases/ts6_errors.txt');
 
 BEGIN {

 	use_ok(BuildTool);
 }

my $pathToSourceCode = "trunk/code";

subtest 'Test Case 6.1' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying that the working copy was created, its files deleted and the correct 
# files exported.
#
####################################################################################
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool";
	my %build_file_rev = ('http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h' => ['9950']);
	my $build_file_rev_ref = \%build_file_rev;
	my $test_directory = getcwd();
	BuildTool->checkOut($build_file_rev_ref,$vcs_path);
	chdir($test_directory);
		
	is(-e "trunk/quals/main_fw/code/shared_system/pack.h","1", "Verifying that the file \"pack.h\" exists");
	system("rm -rf trunk");
};

subtest 'Test Case 6.2' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying that the  build fails if present compile or linker errors
####################################################################################

#------------------------------------------------------------------------
# Compiler/Linker Errors
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/quals/TS6/TS6_comp_error/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $conf = "Debug";
	my $type = "cron";
	
		
	Build->setBuildProper($type,$conf,$build_name);
	Build->buildCode($code_dir,$conf);

	#Verify that the tool will die when there're compiler errors
	is(Build->analyzeCompLinkEW($base_warn_suppress,$conf,$pathToSourceCode),"0", 'Verifying that the tool fails when there are compile errors');
	system("rm -rf trunk");
	
};

subtest 'Test Case 6.3' => sub {
	plan tests => 1;
#########################################################################################
#	
# Verifying that the build lives if present compile or linker warnings, it is a baseline
# and the flag is enabled
#
#########################################################################################

#------------------------------------------------------------------------
# Compiler/Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/quals/TS6/TS6_linker_warn/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $conf = "Release";
	my $base_warn_suppress = "true";
	my $type = "baseline";
	
	Build->setBuildProper($type,$conf,$build_name);
	Build->buildCode($code_dir,$conf);
	#Verify that the tool will live because the flag is enabled	
	lives_ok{Build->analyzeCompLinkEW($base_warn_suppress,$conf,$pathToSourceCode)}, 'Verifying that the tool lives';
	system("rm -rf trunk");

};

subtest 'Test Case 6.4' => sub {
	plan tests => 1;
#########################################################################################
#	
# Verifying that the build fails if present compile or linker warnings, it is a baseline
# and the flag not is enabled
#
#########################################################################################

#------------------------------------------------------------------------
# Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/quals/TS6/TS6_linker_warn/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $conf = "Release";
	my $base_warn_suppress = "false";
	my $type = "baseline";
	
	Build->setBuildProper($type,$conf,$build_name);
	Build->buildCode($code_dir,$conf);
	#Verify that the tool will die when there're linker warnings and the flag is not enabled	
	is(Build->analyzeCompLinkEW($base_warn_suppress,$conf,$pathToSourceCode),"0", 'Verifying that the tool fails');
	system("rm -rf trunk");

};


subtest 'Test Case 6.5' => sub {
	plan tests => 1;
#########################################################################################
#	
# Verifying that the build lives if present compile or linker warnings and it is a cron
#
#########################################################################################

#------------------------------------------------------------------------
# Linker Warnings
#------------------------------------------------------------------------
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/quals/TS6/TS6_linker_warn/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $conf = "Release";
	my $base_warn_suppress = "false";
	my $type = "cron";
	
	Build->setBuildProper($type,$conf,$build_name);
	Build->buildCode($code_dir,$conf);
	#Verify that the tool will live even with the flag disabled becaus the build is a cron	
	is(Build->analyzeCompLinkEW($base_warn_suppress,$conf,$pathToSourceCode),"1", 'Verifying that the tool lives');
	system("rm -rf trunk");
	
};

subtest 'Test Case 6.6' => sub {
	plan tests => 1;
#########################################################################################
#	
# Verifying that the build lives when there are no compiler/linker errors and 
# warnings.
#
#########################################################################################


	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/quals/TS6/TS6_clean_code/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk";
	my $build_name = "fw_builds";
	my $conf = "Debug";
	my $base_warn_suppress = "false";
	my $type = "baseline";
	
	
	Build->setBuildProper($type,$conf,$build_name);
	Build->buildCode($code_dir,$conf);
	#Verify that the tool will live even with the flag disabled because the build is a cron	
	is(Build->analyzeCompLinkEW($base_warn_suppress,$conf,$pathToSourceCode),"1", 'Verifying that the tool lives');
	system("rm -rf trunk");

};

subtest 'Test Case 6.7' => sub {
	plan tests => 9;
#########################################################################################
#	
# Verifying build_rev_info.h - type: cron conf: Debug
#
#########################################################################################

	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk";
	system "svn checkout $vcs_path";
	my $code_dir = "trunk/quals/main_fw";
	my $build_name = "fw_builds";
	my $conf = "Debug";
	my $conf_number = "0";
	my $type = "cron";
	my $number = "2.3";
	my $file = $code_dir . "/code/shared_app/build_rev_info.h";
	
	Build->setBuildProper($type,$conf,$build_name);
	BuildTool->buildCode($code_dir,$type,$number,$conf_number,$conf);
	
	open(FILE,"<$file");
	my @lines = <FILE>;
	is($lines[24],"#define CRON_MAJOR_REV 0x2\n");
	is($lines[25],"#define CRON_MINOR_REV 0x3\n");
	is($lines[31],"#define BUILD_TYPE 0x0\n");

#########################################################################################
#	
# Verifying build_rev_info.h - type: baseline conf: Release
#
#########################################################################################

	my $code_dir = "trunk/quals/main_fw";
	my $build_name = "fw_builds";
	my $conf = "Release";
	my $conf_number = "1";
	my $type = "baseline";
	my $number = "10.1";
	my $file = $code_dir . "/code/shared_app/build_rev_info.h";
	
	Build->setBuildProper($type,$conf,$build_name);
	BuildTool->buildCode($code_dir,$type,$number,$conf_number);
	
	open(FILE,"<$file");
	my @lines = <FILE>;
	is($lines[19],"#define BASE_MAJOR_REV 0xa\n");
	is($lines[20],"#define BASE_MINOR_REV 0x1\n");
	is($lines[31],"#define BUILD_TYPE 0x1\n");

#########################################################################################
#	
# Verifying build_rev_info.h - type: baseline conf: ReleaseRTLx
#
#########################################################################################

	my $code_dir = "trunk/quals/main_fw";
	my $build_name = "fw_builds";
	my $conf = "ReleaseRTLx";
	my $conf_number = "2";
	my $type = "baseline";
	my $number = "1.1";
	my $file = $code_dir . "/code/shared_app/build_rev_info.h";
	
	Build->setBuildProper($type,$conf,$build_name);
	BuildTool->buildCode($code_dir,$type,$number,$conf_number,$conf);
	
	open(FILE,"<$file");
	my @lines = <FILE>;
	is($lines[19],"#define BASE_MAJOR_REV 0x1\n");
	is($lines[20],"#define BASE_MINOR_REV 0x1\n");
	is($lines[31],"#define BUILD_TYPE 0x2\n");
	system("rm -rf trunk");

};

subtest 'Test Case 6.8' => sub {
	plan tests => 3;
#########################################################################################
#	
# Verifying All_except configuration
#
#########################################################################################

#The function from fw_build was copied here, because it's not possible to call it without running
#the entire application because of the use of the Getopt module, that runs the entire fw_build module
#when it's called. 

#Verifying All_except_Debug (one cofiguration)

my %conf_options = ('Debug' => '1',
				'Release' => '2',
				'ReleaseRTLx' => '3');

my @conf_except = ("Debug");
my @build_configuration = ();

foreach my $key (sort keys %conf_options){
$conf = $key;
if(@conf_except) {
			
			for (my $i = 0; $i < scalar(@conf_except);$i++){
					if($conf eq $conf_except[$i]){
						delete $conf_options{$conf};
						last;
					}
			}
} 
if (exists $conf_options{$conf}) {
	push(@build_configuration,$conf);	
}

}

is($build_configuration[0],"Release");
is($build_configuration[1],"ReleaseRTLx");

#Verifying All_except_Release_and_ReleaseRTLx (more than one cofiguration)
my %conf_options = ('Debug' => '1',
				'Release' => '2',
				'ReleaseRTLx' => '3');
my @conf_except = ("Release","ReleaseRTLx");
my @build_configuration = ();

foreach my $key (sort keys %conf_options){
$conf = $key;
if(@conf_except) {
			
			for (my $i = 0; $i < scalar(@conf_except);$i++){
					if($conf eq $conf_except[$i]){
						delete $conf_options{$conf};
						last;
					}
			}
} 
if (exists $conf_options{$conf}) {
	push(@build_configuration,$conf);	
}

}

is($build_configuration[0],"Debug");

};